package org.rscdaemon.ls.packetbuilder;

import org.rscdaemon.ls.LoginEngine;
import org.rscdaemon.ls.net.LSPacket;
import org.rscdaemon.ls.packethandler.PacketHandler;

import java.util.Random;

public class LSPacketBuilder extends StaticPacketBuilder {
	/**
	 * ID of the packet
	 */
	private int pID = 0;
	/**
	 * UID of the packet
	 */
	private long uID = 0;
	/**
	 * Random generator
	 */
	private static Random rand = new Random();

	/**
	 * Sets the ID for this packet.
	 *
	 * @param id The ID of the packet
	 */
	public LSPacketBuilder setID(int pID) {
		this.pID = pID;
		return this;
	}
	
	/**
	 * Sets the uID for this packet.
	 *
	 * @param id The uID of the packet
	 */
	public LSPacketBuilder setUID(long uID) {
		this.uID = uID;
		return this;
	}
	
	/**
	 * Sets the handler for this packet.
	 *
	 * @param handler The handler of the packet
	 */
	public LSPacketBuilder setHandler(LoginEngine engine, PacketHandler handler) {
		uID = rand.nextLong();
		engine.setHandler(uID, handler);
		return this;
	}
	
	/**
	 * Returns a <code>LSPacket</code> object for the data contained
	 * in this builder.
	 *
	 * @return A <code>LSPacket</code> object
	 */
	public LSPacket toPacket() {
		byte[] data = new byte[curLength];
		System.arraycopy(payload, 0, data, 0, curLength);
		return new LSPacket(null, pID, uID, data, bare);
	}

}